package com.seanLab.tool.es;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seanLab.domain.Image;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ailly on 17-8-2.
 */
public class ImageTag {
    @JsonProperty("img_id")
    private String imgID;
    @JsonProperty("img_cate")
    private int imgCate;
    private String imgLocalURL;
    private String imgOriginalURL;
    @JsonProperty("img_source")
    private String imgSource;
    @JsonProperty("Tags")
    private List<ESTagInfo> imgTagsList;

    public String getImgID() {
        return imgID;
    }

    public void setImgID(String imgID) {
        this.imgID = imgID;
    }

    public String getImgLocalURL() {
        return imgLocalURL;
    }

    public void setImgLocalURL(String imgLocalURL) {
        this.imgLocalURL = imgLocalURL;
    }

    public List<ESTagInfo> getImgTagsList() {
        return imgTagsList;
    }

    public void setImgTagsList(List<ESTagInfo> imgTagsList) {
        this.imgTagsList = imgTagsList;
    }

    public ImageTag(String imgID, int imgCate, String imgSource, String imgLocalURL, String imgOriginalURL, List<ESTagInfo> imgTagsList) {
        this.imgID = imgID;
        this.imgCate=imgCate;
        this.imgSource = imgSource;
        this.imgLocalURL = imgLocalURL;
        this.imgOriginalURL = imgOriginalURL;
        this.imgTagsList = imgTagsList;
    }

    public ImageTag() {
    }

    public String buildJson(){
        String s= null;
        try {
            ObjectMapper mapper=new ObjectMapper();
            s = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    public String getImgSource() {
        return imgSource;
    }

    public void setImgSource(String imgSource) {
        this.imgSource = imgSource;
    }
    public int getImgCate(){
        return this.imgCate;
    }
    public void setImgCate(int imgCate){
        this.imgCate=imgCate;
    }

    public String getImgOriginalURL() {
        return imgOriginalURL;
    }

    public void setImgOriginalURL(String imgOriginalURL) {
        this.imgOriginalURL = imgOriginalURL;
    }

    public static ImageTag turntoImageTag(Image img) {
        ImageTag imgtag=new ImageTag();
        imgtag.setImgID(img.getImgID());
        imgtag.setImgLocalURL(img.getImgURL());
        imgtag.setImgOriginalURL(img.getImgOriginalURL());
        imgtag.setImgSource(img.getImgSource());
        imgtag.setImgCate(img.getImgCategory().get(0).getCat_id());
        List<ESTagInfo> l=new ArrayList<ESTagInfo>();
        for (int i=0;i<img.getImgTagList().size();i++){
            l.add(ESTagInfo.turntoESTagInfo(img.getImgTagList().get(i)));
        }
        imgtag.setImgTagsList(l);
//        (img.getImgID(),0,img.getImgSource(),img.getImgURL(),img.getImgOriginalURL(),img.getImgTagList());
        return imgtag;
    }


//    public static String buildJson(Image img){
//
//        List<TagInfo> itl=img.getImgTagList();
//        String tagsList="[";
//        for (int i=0;i<itl.size();i++){
//            tagsList+=itl.get(i).buildJson()+',';
//        }
//        if (tagsList.length()>1){
//            tagsList=tagsList.substring(0,tagsList.length()-1);
//        }
//        tagsList+="]";
//
//        String json = "{" +
//                "\"img_id\":\"" + img.getImgID() +
//                "\",\"img_cate\":\"" + img.getImgCategory()[0]+
//                "\",\"img_source\":\"" + img.getImgSource() +
//                "\",\"imgLocalURL\":\"" + img.getImgURL()+
//                "\",\"imgOriginalURL\":\"" + img.getImgOriginalURL()+
//                "\",\"Tags\":" + tagsList+
//                "}";
//        return json;
//    }
}
